package com.tianma.upload.uploadjartest.socket.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @author lgc
 * @createDate 2020/8/17 - 14:54
 */
public class UdpServer {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] bytes = new byte[1024];

        while (true){
            bytes = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
            socket.receive(datagramPacket);
            System.out.println("ip地址 = " + datagramPacket.getAddress());
            String s = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println("datagramPacket = " + s);
            if (s.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}
